<? 

define('IN_PHPBB', true);
$phpbb_root_path = './../';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
include('login.php');

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_ALI);
init_userprefs($userdata);

	//
	// Start output of page
	//
	
	define('SHOW_ONLINE', true);
	$page_title = $lang['Ali'];
	include($phpbb_root_path . 'includes/page_header.'.$phpEx);
	if ($_POST['Nro']) {
		$campo = explode("@",$_POST['Nro']);
		$sql = "INSERT INTO 6_clasificacion (id, team, color, Puntos, Carreras, Primeros, Segundos, Terceros, Poles, Vrs, Rookie) VALUES ('".$campo[0]."','".$campo[2]."','".$campo[1]."', '0', '0', '0', '0', '0', '0', '0', '".$campo[3]."')";
			
		if ( !($result = $db->sql_query($sql)) ){	
		message_die(GENERAL_ERROR, 'Could not obtain user and group information', '', __LINE__, __FILE__, $sql);
		}
		echo "<script>location.href='editclasi.php'</script>";
	}
	
	$sql = "SELECT z1.user_id, z1.username, z1.user_colour, z1.user_rango, z1.rookie, y1.group_name, y1.group_colour
		FROM " . USERS_TABLE . " z1, " . GROUPS_TABLE . " y1
		WHERE z1.user_colour != '' AND z1.user_colour=y1.group_colour AND m_nopilot != 'si' AND z1.user_id NOT IN (SELECT id FROM 6_clasificacion)
		ORDER BY z1.username ASC";

	if( !($resultado = $db->sql_query($sql)) ){
		message_die(GENERAL_ERROR, 'Could not query users', '', __LINE__, __FILE__, $sql);
	}

	echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';

	$cont = 0;
	while($row = $db->sql_fetchrow($resultado) ){
		$pilotos[$cont] = $row['username'];
		$ident[$cont] = $row['user_id'];
		$colores[$cont] = $row['user_colour'];
		$nomequipo[$cont] = $row['group_name'];
		$novatos[$cont] = $row['rookie'];
		$cont++;
	}

	echo '<select name="Nro">';
	for($i=0;$i<count($pilotos);$i++){
    	echo '<option value="'.$ident[$i].'@'.$colores[$i].'@'.$nomequipo[$i].'@'.$novatos[$i].'">'.$pilotos[$i].'</option>';
	}
	echo '</select>
	<input type="submit" name="submit" value="Enviar"></br>
	
	</form></br>';

	$sql = "SELECT z.user_id, z.username, x.id, x.team, x.color, x.Puntos, x.Carreras, x.Primeros, x.Segundos, x.Terceros, x.Poles, x.Vrs, x.Rookie
		FROM " . USERS_TABLE . " z, 6_clasificacion x
		WHERE z.user_id=x.id
		ORDER BY x.Puntos DESC, x.Primeros DESC, x.Segundos DESC, x.Terceros DESC, x.Poles DESC, x.Vrs DESC, x.Carreras DESC";

	if( !($result = $db->sql_query($sql)) ){
		message_die(GENERAL_ERROR, 'Could not query users', '', __LINE__, __FILE__, $sql);
	}

	$pos = 0;

	echo '<table width="100%" cellpadding="0" cellspacing="0" border="0">
	  	  	<tr>
    			<td width="70" align="center" bgcolor="ffffff">
					pos
				</td>
				<td width="275" bgcolor="ffffff">
					piloto
				</td>
				<td bgcolor="ffffff">
					escuderia
				</td>
				<td width="50" align="center" bgcolor="ffffff">
					puntos
				</td>
    			<td width="50" align="center" bgcolor="ffffff">
					carreras
				</td>
				<td width="50" align="center" bgcolor="ffffff">
					1
				</td>
				<td width="50" align="center" bgcolor="ffffff">
					2
				</td>
				<td width="50" align="center" bgcolor="ffffff">
					3
				</td>
				<td width="50" align="center" bgcolor="ffffff">
					pole
				</td>
				<td width="50" align="center" bgcolor="ffffff">
					vr
				</td>
				</tr>';
	
	while($row = $db->sql_fetchrow($result) ){
		$pos = $pos + 1;
		echo '<tr>
			  	<td align="center" bgcolor="#11043b">
					<span style="color: #ffffff;">'.$pos.'</span>
				</td>
			  	<td bgcolor="'.$row['color'].'">
					'.$row['username'].'
				</td>
			  	<td bgcolor="'.$row['color'].'">
					'.$row['team'].'
				</td>
			  	<td align="center" bgcolor="#3333CC">
					'.$row['Puntos'].'
				</td>
			  	<td align="center" bgcolor="#11043b">
					'.$row['Carreras'].'
				</td>
			  	<td align="center" bgcolor="#bb8d29">
					'.$row['Primeros'].'
				</td>
			  	<td align="center" bgcolor="#958787">
					'.$row['Segundos'].'
				</td>
			  	<td align="center" bgcolor="#603d13">
					'.$row['Terceros'].'
				</td>
			  	<td align="center" bgcolor="#990000">
					'.$row['Poles'].'
				</td>
			  	<td align="center" bgcolor="#003366">
					'.$row['Vrs'].'
				</td>
			  </tr>';
	}

echo '</table>';

//
// Generate the page
//

include('cerrar.php');

?>